﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Data;  

namespace SimpleInterestBiz
{
    class SaacScheduleMethod
    {
        public static string OutputDateFormat(string input_date)
        {
            string _outputdate = "";
            CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            string form = ci.DateTimeFormat.ShortDatePattern;
            char[] sep = ci.DateTimeFormat.DateSeparator.ToCharArray();
            string[] ind = form.Split(sep[0]);
            // int dateIndex = 0;
            //   int monthIndex = 0;
            //   int yearIndex = 0;
            string[] _tempDate = input_date.Split('/');
            _outputdate = "";
            try
            {
                for (var i = 0; i < ind.Length; i++)
                {
                    if (ind[i].Substring(0, 1).ToLower() == "d")
                    {
                        _outputdate += _tempDate[0];
                    }
                    if (ind[i].Substring(0, 1).ToLower() == "m")
                    {
                        _outputdate += _tempDate[1];
                    }
                    if (ind[i].Substring(0, 1).ToLower() == "y")
                    {
                        _outputdate += _tempDate[2];
                    }
                    if (i != (ind.Length - 1))
                    {
                        _outputdate += sep[0];
                    }
                }
            }
            catch (Exception) { }
            return _outputdate;
        }
        /// <summary>
        /// Convert date into format
        /// </summary>
        /// <param name="date">give date as datetime</param>
        /// <returns></returns>
        public static string CDate(DateTime date)
        {
            CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            string form = ci.DateTimeFormat.ShortDatePattern;
            return date.ToString(form);
        }
        private static string getEOM(string date)
        {
            string _EOM = "";
            try
            {
                DateTime _temp = Convert.ToDateTime(date);
                string[] _dateArray = { DateTime.DaysInMonth(_temp.Year, _temp.Month).ToString(), _temp.Month.ToString(), _temp.Year.ToString() };
                CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
                string form = ci.DateTimeFormat.ShortDatePattern;
                char[] sep = ci.DateTimeFormat.DateSeparator.ToCharArray();
                string[] ind = form.Split(sep[0]);
                try
                {
                    for (var i = 0; i < ind.Length; i++)
                    {
                        if (ind[i].Substring(0, 1).ToLower() == "d")
                        {
                            _EOM += _dateArray[0];
                        }
                        if (ind[i].Substring(0, 1).ToLower() == "m")
                        {
                            _EOM += _dateArray[1];
                        }
                        if (ind[i].Substring(0, 1).ToLower() == "y")
                        {
                            _EOM += _dateArray[2];
                        }
                        if (i != (ind.Length - 1))
                        {
                            _EOM += sep[0];
                        }
                    }
                }
                catch (Exception) { }
            }
            catch (Exception) { }
            return _EOM;
        }
        public static string CDate(string Date, string DateFormat)
        {
            string _cdate = "";
            if (DateFormat == "")
            {
                CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
                DateFormat = ci.DateTimeFormat.ShortDatePattern;
            }
            try { _cdate = Convert.ToDateTime(Date).ToString(DateFormat); }
            catch (Exception) { _cdate = Date; }
            return _cdate;
        }
        public static DataTable SaacSchedule(SaacScheduleProperties propObj)
        {
            DataTable _dt=new DataTable();
            _dt.Columns.Add("Paayhash",typeof(int));
            _dt.Columns.Add("date",typeof(DateTime));
            _dt.Columns.Add("Repayment",typeof(double));
            _dt.Columns.Add("Principal",typeof(double));
            _dt.Columns.Add("Interest", typeof(double));
            _dt.Columns.Add("Balance", typeof(double));
            double _Principal=Principal(propObj.Principal,propObj.Estabalishment);
            double _interest=Interest(propObj.Principal,propObj.MonthlyFee);
            DataRow _dr = _dt.NewRow();
            _dr[0] = 0;
            _dr[1] = propObj.LoanDate;
            _dr[2] = 0;
            _dr[3] = _Principal;  
            _dr[4] = _interest; 
            _dr[5] = Balance(_Principal,_interest);
            _dt.Rows.Add(_dr);  
            return _dt;
        }
        public static double Balance(double principal, double interest)
        {
            double _balance = 0;
            _balance = principal + interest;
            return _balance;
        }
        public static double Interest(double basicprincipal, double Monthlyfee)
        {
            double _interest = 0;
            _interest = basicprincipal * (Monthlyfee / 100);
            return _interest; 
        }
        public static double Principal(double basicprincipal, double EstatbalishmentCharge)
        {
            double _principal = 0;
            _principal = basicprincipal * (1 + (EstatbalishmentCharge/100));
            return _principal; 
        }
    }
}
